﻿using AuthenticationMVC.Adapters.Adapters;
using AuthenticationMVC.Adapters.Interfaces;
using AuthenticationMVC.Model;

//Can only use this in a controller (NOT ADAPTER)
using Microsoft.AspNet.Identity; //Needed for User.Identity.GetUserId();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AuthenticationMVC.Controllers
{
    [Authorize]
    public class HomeController : Controller
    {
        private IToDoAdapter _adapter;

        public HomeController()
        {
            _adapter = new ToDoAdapter();
        } 

        public HomeController(IToDoAdapter a)
        {
            _adapter = a;
        } 

        public ActionResult Index()
        {
            string userId = User.Identity.GetUserId();
            List<ToDo> model = _adapter.GetAllToDos(userId);
            return View(model);
        }

        [AllowAnonymous]
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        [AllowAnonymous]
        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
        
        public ActionResult AddToDo()
        {
            return View(new ToDo()); 
        }

        [HttpPost]
        public ActionResult AddToDo(ToDo NewToDo)
        {
            NewToDo.UserId = User.Identity.GetUserId();
            NewToDo.DateCreated = DateTime.Now;
            NewToDo.DateCompleted = null;
            _adapter.CreateToDo(NewToDo);
            return RedirectToAction("Index"); 
        }
    }
}